import { show_mt, obj_int, obj_str, loc_dt, uDb, reload_col, add_ck_col, ck_empty, loc, d_sort, cline } from 'sui';
import { show_val } from 'buy/task/cmd';
import { User } from 'cache';

const get_ck = (cur, main, col) => {
    const ret = {
        sn : v => null,
        custom: v => ck_empty(v),
    };

    add_ck_col(ret, col);

    return ret;
};

const fix_col = () => {
    return [
        {sn: "sn", name: loc("l_488"), tp: "string", ctp: "auto"},
        {sn: "custom", name: loc("l_109"), tp: "string", ctp: "list"},
    ];
};

const load_col = (setCol) => {
    const fix = fix_col();
    reload_col(null, v => setCol(v), "oiltask", fix);
};

const load_custom = (setMain, mapSupply) => {
    uDb(null, "file/list/oiltp", { line: cline() },
        r => {
            if (setMain) {
                d_sort(r, "sn");
                setMain(r);
            }

            const map = {};
            r.forEach(d => map[d.sn] = d);

            mapSupply(map);
        },
        e => {
            if (setMain) {
                setMain(null);
            }

            mapSupply(null);
        });
};

const add_pick = (val, cur, umd, hcmd, path="oilpick") => {
    umd.startWait();

    const num = obj_int(val, "num");
    const dnum = obj_int(cur, "dnum");

    if (num > dnum) {
        umd.setErr(loc("l_753"));
        return;
    }

    uDb(null, "code/gen",
        {
            line: cline(),
            rule: "pick",
        },
        code => {
            const tmp = {
                ...cur,
                bsn: cur["sn"],
                pname: User.uName(),
                num: num,
                pstart: loc_dt(new Date()),
            };

            const sn = obj_str(code, "psn");

            tmp["sn"] = sn;

            const cmd = {
                line: cline(),
                sn: sn,
                v: tmp,
            };

            uDb(null, `file/md/${path}`, cmd,
                r => {
                    umd.endWait();
                    hcmd();
                }, e => umd.setErr());
        }, e => umd.setErr(loc("l_755")));
};

const get_col = (tmp, col, map_supply, map_custom) => {
    col.forEach(d => {
        if (d.sn === "supply") {
            tmp.push({name: d.name, key: d.sn, width: 200, formatter: (props) => show_val(props, "supply", map_supply)});
        } else if (d.sn === "unit") {
            tmp.push({name: d.name, key: d.sn, width: 200, formatter: (props) => show_val(props, "custom", map_custom, "unit")});
        } else if (d.sn === "custom") {
            tmp.push({name: d.name, key: d.sn, width: 200, formatter: (props) => show_val(props, "custom", map_custom, "name")});
        } else {
            show_mt(tmp, d, 160);
        }
    });
};

const get_custom = (map_custom, sn, k) => {
    if (!map_custom || !sn || !k) {
        return "";
    }

    const tmp = map_custom[sn];
    if (!tmp) {
        return "";
    }

    return tmp[k];
};

export {get_custom, get_col, add_pick, load_custom, load_col, get_ck};
